package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import java.util.ArrayList;
import java.util.List;

import cn.mumzone.basic.adapter.rv.CommonAdapter;
import cn.mumzone.basic.adapter.rv.ViewHolder;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.ReturnGoodsEntity;

/**
 * Created by JoeLjt on 2019/6/28.
 * Email: lijiateng1219@gmail.com
 * Description:
 */

public class ADA_DialogChooseReturnGoods extends CommonAdapter<ReturnGoodsEntity> {

    public ADA_DialogChooseReturnGoods(Context context) {
        super(context);
    }

    @Override
    protected int itemLayoutId() {
        return R.layout.item_choose_return_goods;
    }

    @Override
    protected void convert(ViewHolder holder, final ReturnGoodsEntity goodsEntity, int position) {
        holder.setText(R.id.tv_good_name, goodsEntity.getGoodsName());

        holder.setText(R.id.tv_good_spec, getGoodSpecString(goodsEntity.getGoodsSpec()));

//        if (goodsEntity.getRealPay().equals(goodsEntity.getPrice())) {
//            holder.setVisible(R.id.tv_good_origin_price, false);
//        } else {
//            holder.setVisible(R.id.tv_good_origin_price, true);
//
//            TextView tvOriginPrice = holder.getView(R.id.tv_good_origin_price);
//            tvOriginPrice.setText("￥" + goodsEntity.getPrice());
//            tvOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
//        }
        holder.setText(R.id.tv_good_price, "￥" + goodsEntity.getPrice());

        CheckBox cbChoose = holder.getView(R.id.rb_choose_goods);
        cbChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                goodsEntity.setSelectCount(isChecked ? 1 : 0);
                goodsEntity.setSelectUnRegisterCount(isChecked ? 1 : 0);
                if (mListener != null) {
                    mListener.onCheckChanged();
                }
            }
        });

    }

    public ArrayList<ReturnGoodsEntity> getSelectedList() {
        ArrayList<ReturnGoodsEntity> goodsList = new ArrayList<>();
        if (!ListUtil.isListEmpty(getDataList())) {
            for (ReturnGoodsEntity entity : getDataList()) {
                if (entity.getSelectCount() > 0) {
                    goodsList.add(entity);
                }
            }
        }
        return goodsList;
    }

    public int getSelectedCount() {
        int totalCount = 0;
        ArrayList<ReturnGoodsEntity> selectedList = getSelectedList();
        for (ReturnGoodsEntity goodsEntity : selectedList) {
            totalCount += goodsEntity.getSelectCount();
        }
        return totalCount;
    }

    public interface OnCheckedChangedListener {
        void onCheckChanged();
    }

    private OnCheckedChangedListener mListener;

    public void setOnCheckedChangedListener(OnCheckedChangedListener mListener) {
        this.mListener = mListener;
    }

    private String getGoodSpecString(List<ReturnGoodsEntity.GoodsSpecBean> goodsSpec) {
        StringBuilder builder = new StringBuilder();
        builder.append("规格：");
        for (int i = 0; i < goodsSpec.size(); i++) {
            ReturnGoodsEntity.GoodsSpecBean goodsSpecBean = goodsSpec.get(i);
            builder.append(goodsSpecBean.getValue());
            if (i < goodsSpec.size() - 1) {
                builder.append(",");
            }
        }
        return builder.toString();
    }

}
